﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKRGeometry;
using QuickModel.InteractiveOperation;

namespace QuickModel.AxisTransModel
{
	public class AxisManager
	{
		internal static List<string> GetDocAxisTypes(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			IList<Element> list2 = filteredElementCollector.OfClass(typeof(GridType)).ToElements();
			foreach (Element element in list2)
			{
				GridType gridType = element as GridType;
				bool flag = gridType != null;
				if (flag)
				{
					list.Add(gridType.Name);
				}
			}
			return list;
		}

		internal static List<string> GetDocAxisNumbers(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			IList<Element> list2 = filteredElementCollector.OfClass(typeof(Grid)).ToElements();
			foreach (Element element in list2)
			{
				Grid grid = element as Grid;
				bool flag = grid != null;
				if (flag)
				{
					bool flag2 = !list.Contains(grid.Name);
					if (flag2)
					{
						list.Add(grid.Name);
					}
				}
			}
			return list;
		}

		internal static List<Curve> GetAxisCurves(List<ICadElement> lstCadElemInfo, List<string> lstAxisLineLayer)
		{
			List<Curve> list = new List<Curve>();
			foreach (ICadElement cadElement in lstCadElemInfo)
			{
				bool flag = cadElement is CurveInfo;
				if (flag)
				{
					CurveInfo curveInfo = cadElement as CurveInfo;
					bool flag2 = lstAxisLineLayer.Contains(curveInfo.UseLayerName);
					if (flag2)
					{
						bool flag3 = !AxisManager.IsCircle(curveInfo.ThisCurve);
						if (flag3)
						{
							list.Add(curveInfo.ThisCurve);
						}
					}
				}
			}
			return list;
		}

		internal static List<Curve> GetAxisNumberCurves(List<ICadElement> lstCadElemInfo, List<string> lstAxisNumberLayer)
		{
			List<Curve> list = new List<Curve>();
			foreach (ICadElement cadElement in lstCadElemInfo)
			{
				bool flag = cadElement is CurveInfo;
				if (flag)
				{
					CurveInfo curveInfo = cadElement as CurveInfo;
					bool flag2 = lstAxisNumberLayer.Contains(curveInfo.UseLayerName);
					if (flag2)
					{
						list.Add(curveInfo.ThisCurve);
					}
				}
			}
			return list;
		}

		internal static List<TextInfo> GetAxisNumberTextes(List<ICadElement> lstCadElemInfo, List<string> lstAxisNumberLayer)
		{
			List<TextInfo> list = new List<TextInfo>();
			foreach (ICadElement cadElement in lstCadElemInfo)
			{
				bool flag = cadElement is TextInfo;
				if (flag)
				{
					TextInfo textInfo = cadElement as TextInfo;
					bool flag2 = string.IsNullOrWhiteSpace(textInfo.UseText);
					if (!flag2)
					{
						bool flag3 = lstAxisNumberLayer.Contains(textInfo.UseLayerName);
						if (flag3)
						{
							list.Add(textInfo);
						}
					}
				}
			}
			return list;
		}

		internal static List<AxisNumber> GetAxisNumber(List<Curve> lstAxisNumberCurve, List<TextInfo> lstAxisNumberText)
		{
			List<AxisNumber> list = new List<AxisNumber>();
			for (int i = 0; i < lstAxisNumberCurve.Count; i++)
			{
				bool flag = AxisManager.IsCircle(lstAxisNumberCurve[i]);
				if (flag)
				{
					Arc arc = lstAxisNumberCurve[i] as Arc;
					AxisNumber axisNumber = new AxisNumber();
					axisNumber.Center = arc.Center;
					axisNumber.Radius = arc.Radius;
					axisNumber.CircleCurve = arc;
					List<TextInfo> axisNumberTextInfo = AxisManager.GetAxisNumberTextInfo(axisNumber, lstAxisNumberText);
					bool flag2 = axisNumberTextInfo != null && axisNumberTextInfo.Count > 0;
					if (flag2)
					{
						axisNumber.Number = AxisManager.GetnTextInfoString(axisNumberTextInfo);
						list.Add(axisNumber);
					}
					lstAxisNumberCurve.RemoveAt(i);
					i--;
				}
			}
			return list;
		}

		internal static void MergeAxisCurves(bool bIsLinked, List<Curve> lstAxisLine)
		{
			for (int i = 0; i < lstAxisLine.Count - 1; i++)
			{
				Curve value = lstAxisLine[i];
				for (int j = i + 1; j < lstAxisLine.Count; j++)
				{
					Curve mergeCurve = lstAxisLine[j];
					bool flag = AxisManager.MergeTwoCurve(bIsLinked, mergeCurve, ref value);
					if (flag)
					{
						lstAxisLine.RemoveAt(j);
						j--;
					}
				}
				lstAxisLine[i] = value;
			}
		}

		internal static void ResetExtendCurvesPoint(List<Curve> lstAxisCurve, List<Curve> lstExtendCurve)
		{
			for (int i = 0; i < lstExtendCurve.Count; i++)
			{
				Curve curve = lstExtendCurve[i];
				bool flag = curve is Line;
				if (flag)
				{
					Line value = curve as Line;
					bool flag2 = AxisManager.RealLinkCurve(lstAxisCurve, ref value) || AxisManager.ResetCurvesPoint(lstAxisCurve, ref value);
					if (flag2)
					{
						lstExtendCurve[i] = value;
					}
				}
			}
		}

		internal static void MergeExtendToAxisCurves(List<Curve> lstAxisCurve, List<Curve> lstExtendCurve)
		{
			for (int i = 0; i < lstAxisCurve.Count; i++)
			{
				Curve curve = lstAxisCurve[i];
				bool flag = curve is Line;
				if (flag)
				{
					bool flag2 = AxisManager.MergeExtendToAxisLine(ref curve, lstExtendCurve);
					if (flag2)
					{
						lstAxisCurve[i] = curve;
					}
				}
			}
		}

		private static bool MergeExtendToAxisLine(ref Curve axisLine, List<Curve> lstExtendCurve)
		{
			bool result = false;
			for (int i = 0; i < lstExtendCurve.Count; i++)
			{
				bool flag = AxisManager.MergeTwoLine(true, lstExtendCurve[i], ref axisLine);
				if (flag)
				{
					lstExtendCurve.RemoveAt(i);
					result = true;
					i--;
				}
			}
			return result;
		}

		internal static void MergeOverlapCurves(List<Curve> lstCurve)
		{
			List<Line> list = new List<Line>();
			for (int i = 0; i < lstCurve.Count; i++)
			{
				bool flag = lstCurve[i] is Line;
				if (flag)
				{
					Line item = lstCurve[i] as Line;
					list.Add(item);
					lstCurve.RemoveAt(i);
					i--;
				}
			}
			AxisManager.MergeOverlapLines(list);
			lstCurve.AddRange(list);
		}

		internal static List<Curve> GetAxisCurveLoop(Curve firstCurve, List<Curve> lstCurve)
		{
			List<Curve> list = new List<Curve>();
			list.Add(firstCurve);
			bool flag = lstCurve.Count < 1;
			List<Curve> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				XYZ endPoint = firstCurve.GetEndPoint(0);
				XYZ endPoint2 = firstCurve.GetEndPoint(1);
				Curve nextCurve = AxisManager.GetNextCurve(lstCurve, firstCurve, ref endPoint);
				while (nextCurve != null)
				{
					list.Insert(0, nextCurve);
					nextCurve = AxisManager.GetNextCurve(lstCurve, nextCurve, ref endPoint);
				}
				Curve nextCurve2 = AxisManager.GetNextCurve(lstCurve, firstCurve, ref endPoint2);
				while (nextCurve2 != null)
				{
					list.Add(nextCurve2);
					nextCurve2 = AxisManager.GetNextCurve(lstCurve, nextCurve2, ref endPoint2);
				}
				foreach (Curve item in list)
				{
					bool flag2 = lstCurve.Contains(item);
					if (flag2)
					{
						lstCurve.Remove(item);
					}
				}
				result = AxisManager.LoopCurves(list, endPoint);
			}
			return result;
		}

		internal static AxisNumber GetLinkAxisNumber(Curve curve, int nCurveEnd, List<AxisNumber> lstAxisNumber)
		{
			foreach (AxisNumber axisNumber in lstAxisNumber)
			{
				bool flag = AxisManager.CurveLinkdNumber(axisNumber, curve);
				if (flag)
				{
					XYZ endPoint = curve.GetEndPoint(0);
					XYZ endPoint2 = curve.GetEndPoint(1);
					bool flag2 = endPoint.DistanceTo(axisNumber.Center) < endPoint2.DistanceTo(axisNumber.Center);
					if (flag2)
					{
						bool flag3 = nCurveEnd == 0;
						if (flag3)
						{
							return axisNumber;
						}
					}
					else
					{
						bool flag4 = nCurveEnd == 1;
						if (flag4)
						{
							return axisNumber;
						}
					}
				}
			}
			return null;
		}

		internal static bool IsCircle(Curve curveItem)
		{
			bool flag = curveItem == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				Arc arc = curveItem as Arc;
				bool flag2 = arc == null;
				if (flag2)
				{
					result = false;
				}
				else
				{
					double val = Math.PI*2 * arc.Radius;
					result = Geometry.IsEqual(val, arc.Length, AxisManager.m_dTolerance);
				}
			}
			return result;
		}

		internal static string GetAxisNumber(string strNumber, List<string> lstAllAxisNumber)
		{
			bool flag = !lstAllAxisNumber.Contains(strNumber);
			string result;
			if (flag)
			{
				lstAllAxisNumber.Add(strNumber);
				result = strNumber;
			}
			else
			{
				int num = 1;
				string text;
				for (;;)
				{
					text = string.Format("{0}-{1}", strNumber, num);
					bool flag2 = !lstAllAxisNumber.Contains(text);
					if (flag2)
					{
						break;
					}
					num++;
				}
				lstAllAxisNumber.Add(text);
				result = text;
			}
			return result;
		}

		internal static string GetMaxNumber(List<string> lstAllAxisNumber)
		{
			int num = -1;
			string result = string.Empty;
			foreach (string text in lstAllAxisNumber)
			{
				int num2 = text.IndexOf("-");
				string text2 = (num2 < 0) ? text : text.Substring(0, num2);
				bool flag = AxisManager.IsNumber(text2);
				if (flag)
				{
					int num3 = Convert.ToInt32(text2);
					bool flag2 = num < num3;
					if (flag2)
					{
						num = num3;
						result = text2;
					}
				}
			}
			return result;
		}

		internal static string GetMaxAlpha(List<string> lstAllAxisNumber)
		{
			int num = -1;
			string result = string.Empty;
			foreach (string text in lstAllAxisNumber)
			{
				int num2 = text.IndexOf("-");
				string text2 = (num2 < 0) ? text : text.Substring(0, num2);
				bool flag = AxisManager.IsAlpha(text2);
				if (flag)
				{
					int num3 = AxisManager.AlphaToInt(text2);
					bool flag2 = num < num3;
					if (flag2)
					{
						num = num3;
						result = text2;
					}
				}
			}
			return result;
		}

		internal static bool IsNumber(string str)
		{
			bool flag = str.Length == 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				foreach (char c in str.ToCharArray())
				{
					bool flag2 = !char.IsDigit(c);
					if (flag2)
					{
						return false;
					}
				}
				result = true;
			}
			return result;
		}

		internal static bool IsAlpha(string str)
		{
			bool flag = str.Length == 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				foreach (char c in str.ToCharArray())
				{
					bool flag2 = (c < 'A' || c > 'Z') && (c < 'a' || c > 'z');
					if (flag2)
					{
						return false;
					}
				}
				result = true;
			}
			return result;
		}

		internal static int AlphaToInt(string strAlpha)
		{
			int num = 0;
			string text = strAlpha.ToUpper();
			int i = text.Length - 1;
			int num2 = 1;
			while (i >= 0)
			{
				char c = strAlpha[i];
				num += (int)(c - '@') * num2;
				i--;
				num2 *= 26;
			}
			return num;
		}

		internal static string IntToAlpha(int nValue)
		{
			string text = string.Empty;
			while (nValue > 0)
			{
				int num = nValue % 26;
				bool flag = num == 0;
				if (flag)
				{
					num = 26;
				}
				text = ((char)(num + 64)).ToString() + text;
				nValue = (nValue - num) / 26;
			}
			return text;
		}

		private static List<TextInfo> GetAxisNumberTextInfo(AxisNumber axisNumber, List<TextInfo> lstAxisNumberText)
		{
			XYZ center = axisNumber.Center;
			double radius = axisNumber.Radius;
			List<TextInfo> list = new List<TextInfo>();
			for (int i = 0; i < lstAxisNumberText.Count; i++)
			{
				TextInfo textInfo = lstAxisNumberText[i];
				bool flag = textInfo.ActualBox.Count == 4;
				if (flag)
				{
					XYZ xyz = Geometry.CalculatMidPoint(textInfo.ActualBox[0], textInfo.ActualBox[3]);
					bool flag2 = Geometry.LessThan(center.DistanceTo(xyz), radius);
					if (flag2)
					{
						list.Add(textInfo);
						lstAxisNumberText.RemoveAt(i);
						i--;
					}
				}
			}
			return list;
		}

		private static string GetnTextInfoString(List<TextInfo> lstInfo)
		{
			bool flag = lstInfo.Count == 1;
			string result;
			if (flag)
			{
				result = lstInfo[0].UseText;
			}
			else
			{
				List<KeyValuePair<XYZ, string>> list = new List<KeyValuePair<XYZ, string>>();
				foreach (TextInfo textInfo in lstInfo)
				{
					bool flag2 = textInfo.ActualBox.Count == 4;
					if (flag2)
					{
						XYZ key = Geometry.CalculatMidPoint(textInfo.ActualBox[0], textInfo.ActualBox[3]);
						KeyValuePair<XYZ, string> item = new KeyValuePair<XYZ, string>(key, textInfo.UseText);
						list.Add(item);
					}
				}
				bool flag3 = list.Count == 0;
				if (flag3)
				{
					result = lstInfo[0].UseText;
				}
				else
				{
					bool flag4 = list.Count == 1;
					if (flag4)
					{
						result = list[0].Value;
					}
					else
					{
						list.Sort((KeyValuePair<XYZ, string> t1, KeyValuePair<XYZ, string> t2) => t1.Key.X.CompareTo(t2.Key.X));
						string text = list[0].Value;
						for (int i = 1; i < list.Count; i++)
						{
							text = text + "/" + list[i].Value;
						}
						result = text;
					}
				}
			}
			return result;
		}

		private static bool MergeTwoCurve(bool bIsLinked, Curve mergeCurve, ref Curve resultCurve)
		{
			bool flag = mergeCurve.GetType() == typeof(Line) && resultCurve.GetType() == typeof(Line);
			bool result;
			if (flag)
			{
				result = AxisManager.MergeTwoLine(bIsLinked, mergeCurve, ref resultCurve);
			}
			else
			{
				bool flag2 = mergeCurve.GetType() == typeof(Arc) && resultCurve.GetType() == typeof(Arc);
				result = (flag2 && AxisManager.MergeTwoArc(bIsLinked, mergeCurve, ref resultCurve));
			}
			return result;
		}

		private static bool MergeTwoArc(bool bIsLinked, Curve mergeArc, ref Curve resultArc)
		{
			Arc arc = mergeArc as Arc;
			Arc arc2 = resultArc as Arc;
			bool flag = arc == null || arc2 == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = !Geometry.IsEqual(arc.Center, arc2.Center, AxisManager.m_dTolerance);
				if (flag2)
				{
					result = false;
				}
				else
				{
					bool flag3 = !Geometry.IsEqual(arc.Radius, arc2.Radius, AxisManager.m_dTolerance);
					if (flag3)
					{
						result = false;
					}
					else
					{
						XYZ endPoint = arc.GetEndPoint(0);
						XYZ endPoint2 = arc.GetEndPoint(1);
						XYZ center = arc.Center;
						XYZ normal = arc.Normal;
						XYZ endPoint3 = arc2.GetEndPoint(0);
						XYZ endPoint4 = arc2.GetEndPoint(1);
						XYZ center2 = arc2.Center;
						XYZ normal2 = arc2.Normal;
						XYZ xyz = null;
						XYZ xyz2 = null;
						Geometry.OverlapType overlapType = Geometry.MergerOfTwoArc(endPoint, endPoint2, center, normal, endPoint3, endPoint4, center2, normal2, ref xyz, ref xyz2, AxisManager.m_dTolerance);
						bool flag4 = overlapType != Geometry.OverlapType._OT_None && xyz != null && xyz2 != null;
						if (flag4)
						{
							XYZ xyz3 = Geometry.CalculatMidPoint(xyz, xyz2, center2, normal2);
							resultArc = Arc.Create(endPoint, endPoint2, xyz3);
							result = true;
						}
						else
						{
							result = false;
						}
					}
				}
			}
			return result;
		}

		private static bool MergeTwoLine(bool bIsLinked, Curve mergeLine, ref Curve resultLine)
		{
			Line line = mergeLine as Line;
			Line line2 = resultLine as Line;
			bool flag = line == null || line2 == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				XYZ endPoint = line.GetEndPoint(0);
				XYZ endPoint2 = line.GetEndPoint(1);
				XYZ endPoint3 = line2.GetEndPoint(0);
				XYZ endPoint4 = line2.GetEndPoint(1);
				XYZ xyz = Geometry.CalculateFootPoint(endPoint3, endPoint4, endPoint);
				XYZ xyz2 = Geometry.CalculateFootPoint(endPoint3, endPoint4, endPoint2);
				bool flag2 = Geometry.GreaterThan(endPoint.DistanceTo(xyz), AxisManager.m_dTolerance) || Geometry.GreaterThan(endPoint2.DistanceTo(xyz2), AxisManager.m_dTolerance);
				if (flag2)
				{
					result = false;
				}
				else
				{
					List<XYZ> list = new List<XYZ>();
					list.Add(endPoint3);
					list.Add(endPoint4);
					list.Add(xyz);
					list.Add(xyz2);
					XYZ vtLine = (endPoint4 - endPoint3).Normalize();
					AxisManager.SortPoints(list, vtLine);
					XYZ xyz3 = list.First<XYZ>();
					XYZ xyz4 = list.Last<XYZ>();
					if (bIsLinked)
					{
						double num = endPoint.DistanceTo(endPoint2);
						double num2 = endPoint3.DistanceTo(endPoint4);
						double val = xyz3.DistanceTo(xyz4);
						bool flag3 = Geometry.GreaterThan(val, num + num2);
						if (flag3)
						{
							return false;
						}
					}
					resultLine = Line.CreateBound(xyz3, xyz4);
					result = true;
				}
			}
			return result;
		}

		private static List<Curve> LoopCurves(List<Curve> lstCurve, XYZ ptCurvePos)
		{
			List<Curve> list = new List<Curve>();
			for (int i = 0; i < lstCurve.Count; i++)
			{
				Curve curve = lstCurve[i];
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				bool flag = endPoint.DistanceTo(ptCurvePos) < endPoint2.DistanceTo(ptCurvePos);
				if (flag)
				{
					bool flag2 = !Geometry.IsEqual(endPoint, ptCurvePos);
					if (flag2)
					{
						list.Add(AxisManager.ResetCurvePoint(curve, 0, ptCurvePos));
					}
					else
					{
						list.Add(curve);
					}
					ptCurvePos = endPoint2;
				}
				else
				{
					Curve curve2 = curve.CreateReversed();
					bool flag3 = !Geometry.IsEqual(endPoint2, ptCurvePos);
					if (flag3)
					{
						list.Add(AxisManager.ResetCurvePoint(curve2, 0, ptCurvePos));
					}
					else
					{
						list.Add(curve2);
					}
					ptCurvePos = endPoint;
				}
			}
			return list;
		}

		private static Curve GetNextCurve(List<Curve> lstCurve, Curve currentCurve, ref XYZ ptNext)
		{
			List<Curve> list = new List<Curve>();
			XYZ endPoint = currentCurve.GetEndPoint(0);
			XYZ endPoint2 = currentCurve.GetEndPoint(1);
			foreach (Curve curve in lstCurve)
			{
				XYZ endPoint3 = curve.GetEndPoint(0);
				XYZ endPoint4 = curve.GetEndPoint(1);
				bool flag = Geometry.IsEqual(endPoint, endPoint3) && Geometry.IsEqual(endPoint2, endPoint4);
				if (!flag)
				{
					bool flag2 = Geometry.IsEqual(ptNext, endPoint3, AxisManager.m_dPointEqual) || Geometry.IsEqual(ptNext, endPoint4, AxisManager.m_dPointEqual);
					if (flag2)
					{
						list.Add(curve);
					}
				}
			}
			bool flag3 = list.Count == 1;
			Curve result;
			if (flag3)
			{
				Curve curve2 = list[0];
				XYZ endPoint5 = curve2.GetEndPoint(0);
				XYZ endPoint6 = curve2.GetEndPoint(1);
				ptNext = (Geometry.IsEqual(ptNext, endPoint5, AxisManager.m_dPointEqual) ? endPoint6 : endPoint5);
				result = curve2;
			}
			else
			{
				result = null;
			}
			return result;
		}

		private static bool ResetCurvesPoint(List<Curve> lstAllCurve, ref Line firstLine)
		{
			bool flag = false;
			XYZ xyz = firstLine.GetEndPoint(0);
			XYZ xyz2 = firstLine.GetEndPoint(1);
			for (int i = 0; i < lstAllCurve.Count; i++)
			{
				Curve curve = lstAllCurve[i];
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				bool flag2 = AxisManager.IsPointInLine(xyz, xyz2, endPoint);
				bool flag3 = AxisManager.IsPointInLine(xyz, xyz2, endPoint2);
				bool flag4 = flag2;
				if (flag4)
				{
					bool flag5 = flag3;
					if (flag5)
					{
						bool flag6 = curve is Line;
						if (flag6)
						{
							lstAllCurve.RemoveAt(i);
							i--;
						}
					}
					else
					{
						bool flag7 = curve is Arc;
						if (flag7)
						{
							Arc arc = curve as Arc;
							XYZ vt = (endPoint - arc.Center).Normalize();
							bool flag8 = AxisManager.IsVertical(vt, firstLine.Direction);
							if (flag8)
							{
								AxisManager.ResetLinePoint(endPoint, ref xyz, ref xyz2);
								flag = true;
							}
						}
						else
						{
							bool flag9 = curve is Line;
							if (flag9)
							{
								XYZ vt2 = (endPoint2 - endPoint).Normalize();
								bool flag10 = AxisManager.IsParalel(vt2, firstLine.Direction);
								if (flag10)
								{
									bool flag11 = AxisManager.IsPointInLine(endPoint, endPoint2, xyz);
									if (flag11)
									{
										xyz = endPoint;
									}
									else
									{
										xyz2 = endPoint;
									}
									flag = true;
								}
							}
						}
					}
				}
				else
				{
					bool flag12 = flag3;
					if (flag12)
					{
						bool flag13 = curve is Arc;
						if (flag13)
						{
							Arc arc2 = curve as Arc;
							XYZ vt3 = (endPoint2 - arc2.Center).Normalize();
							bool flag14 = AxisManager.IsVertical(vt3, firstLine.Direction);
							if (flag14)
							{
								AxisManager.ResetLinePoint(endPoint2, ref xyz, ref xyz2);
								flag = true;
							}
						}
						else
						{
							bool flag15 = curve is Line;
							if (flag15)
							{
								XYZ vt4 = (endPoint2 - endPoint).Normalize();
								bool flag16 = AxisManager.IsParalel(vt4, firstLine.Direction);
								if (flag16)
								{
									bool flag17 = AxisManager.IsPointInLine(endPoint, endPoint2, xyz);
									if (flag17)
									{
										xyz = endPoint2;
									}
									else
									{
										xyz2 = endPoint2;
									}
									flag = true;
								}
							}
						}
					}
				}
			}
			bool flag18 = flag;
			if (flag18)
			{
				firstLine = Line.CreateBound(xyz, xyz2);
			}
			return flag;
		}

		private static Curve ResetCurvePoint(Curve oldCurve, int nSetPointIndex, XYZ ptNewPos)
		{
			XYZ endPoint = oldCurve.GetEndPoint(0);
			XYZ endPoint2 = oldCurve.GetEndPoint(1);
			bool flag = oldCurve is Line;
			if (flag)
			{
				bool flag2 = nSetPointIndex == 0;
				if (flag2)
				{
					return Line.CreateBound(ptNewPos, endPoint2);
				}
				bool flag3 = nSetPointIndex == 1;
				if (flag3)
				{
					return Line.CreateBound(endPoint, ptNewPos);
				}
			}
			else
			{
				bool flag4 = oldCurve is Arc;
				if (flag4)
				{
					XYZ xyz = oldCurve.Evaluate(0.5, true);
					bool flag5 = nSetPointIndex == 0;
					if (flag5)
					{
						return Arc.Create(ptNewPos, endPoint2, xyz);
					}
					bool flag6 = nSetPointIndex == 1;
					if (flag6)
					{
						return Arc.Create(endPoint, ptNewPos, xyz);
					}
				}
			}
			return oldCurve;
		}

		private static bool RealLinkCurve(List<Curve> lstAllCurve, ref Line firstLine)
		{
			bool flag = false;
			XYZ xyz = firstLine.GetEndPoint(0);
			XYZ xyz2 = firstLine.GetEndPoint(1);
			for (int i = 0; i < lstAllCurve.Count; i++)
			{
				Curve curve = lstAllCurve[i];
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				bool flag2 = Geometry.LessThan(xyz.DistanceTo(endPoint), AxisManager.m_dPointEqual);
				if (flag2)
				{
					xyz = endPoint;
					flag = true;
					break;
				}
				bool flag3 = Geometry.LessThan(xyz.DistanceTo(endPoint2), AxisManager.m_dPointEqual);
				if (flag3)
				{
					xyz = endPoint2;
					flag = true;
					break;
				}
				bool flag4 = Geometry.LessThan(xyz2.DistanceTo(endPoint), AxisManager.m_dPointEqual);
				if (flag4)
				{
					xyz2 = endPoint;
					flag = true;
					break;
				}
				bool flag5 = Geometry.LessThan(xyz2.DistanceTo(endPoint2), AxisManager.m_dPointEqual);
				if (flag5)
				{
					xyz2 = endPoint2;
					flag = true;
					break;
				}
			}
			bool flag6 = flag;
			if (flag6)
			{
				firstLine = Line.CreateBound(xyz, xyz2);
			}
			return flag;
		}

		private static void ResetLinePoint(XYZ ptNewEnd, ref XYZ ptLineS, ref XYZ ptLineE)
		{
			bool flag = ptLineS.DistanceTo(ptNewEnd) < ptLineE.DistanceTo(ptNewEnd);
			if (flag)
			{
				ptLineS = ptNewEnd;
			}
			else
			{
				ptLineE = ptNewEnd;
			}
		}

		private static void MergeOverlapLines(List<Line> lstLine)
		{
			bool flag = lstLine == null;
			if (!flag)
			{
				for (int i = 0; i < lstLine.Count - 1; i++)
				{
					Line line = lstLine[i];
					XYZ xyz = line.GetEndPoint(0);
					XYZ xyz2 = line.GetEndPoint(1);
					bool flag2 = false;
					for (int j = i + 1; j < lstLine.Count; j++)
					{
						Line line2 = lstLine[j];
						XYZ endPoint = line2.GetEndPoint(0);
						XYZ endPoint2 = line2.GetEndPoint(1);
						XYZ xyz3 = null;
						XYZ xyz4 = null;
						bool overlapLinePoint = AxisManager.GetOverlapLinePoint(xyz, xyz2, endPoint, endPoint2, ref xyz3, ref xyz4);
						if (overlapLinePoint)
						{
							lstLine.RemoveAt(j);
							j--;
							xyz = xyz3;
							xyz2 = xyz4;
							flag2 = true;
						}
					}
					bool flag3 = flag2;
					if (flag3)
					{
						lstLine[i] = Line.CreateBound(xyz, xyz2);
					}
				}
			}
		}

		private static bool GetOverlapLinePoint(XYZ ptS1, XYZ ptE1, XYZ ptS2, XYZ ptE2, ref XYZ ptMergeS, ref XYZ ptMergeE)
		{
			XYZ xyz = Geometry.CalculateFootPoint(ptS1, ptE1, ptS2);
			XYZ xyz2 = Geometry.CalculateFootPoint(ptS1, ptE1, ptE2);
			bool flag = Geometry.GreaterThan(ptS2.DistanceTo(xyz), AxisManager.m_dTolerance) || Geometry.GreaterThan(ptE2.DistanceTo(xyz2), AxisManager.m_dTolerance);
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				List<XYZ> list = new List<XYZ>();
				list.Add(ptS1);
				list.Add(ptE1);
				list.Add(xyz);
				list.Add(xyz2);
				XYZ vtLine = (ptE1 - ptS1).Normalize();
				AxisManager.SortPoints(list, vtLine);
				XYZ xyz3 = list.First<XYZ>();
				XYZ xyz4 = list.Last<XYZ>();
				double num = ptS1.DistanceTo(ptE1);
				double num2 = ptS2.DistanceTo(ptE2);
				double val = xyz3.DistanceTo(xyz4);
				bool flag2 = Geometry.LessThan(val, num + num2);
				if (flag2)
				{
					ptMergeS = xyz3;
					ptMergeE = xyz4;
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		private static bool IsPointInLine(XYZ ptLineS, XYZ ptLineE, XYZ ptTest)
		{
			double num = ptLineS.DistanceTo(ptLineE);
			double num2 = ptLineS.DistanceTo(ptTest);
			double num3 = ptLineE.DistanceTo(ptTest);
			return !Geometry.GreaterThan(num2 + num3 - num, AxisManager.m_dTolerance);
		}

		private static bool IsParalel(XYZ vt1, XYZ vt2)
		{
			double val = vt1.AngleTo(vt2);
			return Geometry.LessThan(val, AxisManager.m_dTolerance) || Geometry.IsEqual(val, Math.PI, AxisManager.m_dTolerance);
		}

		private static bool IsVertical(XYZ vt1, XYZ vt2)
		{
			double val = vt1.AngleTo(vt2);
			return Geometry.IsEqual(val, Math.PI*.5, AxisManager.m_dTolerance);
		}

		private static bool CurveLinkdNumber(AxisNumber number, Curve linkCurve)
		{
			XYZ endPoint = linkCurve.GetEndPoint(0);
			double num = endPoint.DistanceTo(number.Center);
			bool flag = Geometry.LessThan(num - number.Radius, AxisManager.m_dTolerance);
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				XYZ endPoint2 = linkCurve.GetEndPoint(1);
				num = endPoint2.DistanceTo(number.Center);
				bool flag2 = Geometry.LessThan(num - number.Radius, AxisManager.m_dTolerance);
				result = flag2;
			}
			return result;
		}

		private static void SortPoints(List<XYZ> lstPt, XYZ vtLine)
		{
			for (int i = 0; i < lstPt.Count - 1; i++)
			{
				for (int j = i + 1; j < lstPt.Count; j++)
				{
					XYZ pt = (lstPt[j] - lstPt[i]).Normalize();
					bool flag = !Geometry.IsEqual(vtLine, pt);
					if (flag)
					{
						XYZ value = lstPt[i];
						lstPt[i] = lstPt[j];
						lstPt[j] = value;
					}
				}
			}
		}

		private static double m_dTolerance = ToleranceManger.GetToleranceManger().GetTolerance("轴网公差");

		private static double m_dPointEqual = ToleranceManger.GetToleranceManger().GetTolerance("轴线相连");
	}
}
